package lc.q250_300;

public class Q279 {

    public static void main(String[] args) {
        Q279 q279 = new Q279();
        System.out.println(q279.numSquares(7));
    }

    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            int sq = (int) Math.sqrt(i);
            int min = Integer.MAX_VALUE;
            for (int j = sq; j >= 1; j--) {
                min = Integer.min(min, dp[i - j * j]);
            }
            dp[i] = min + 1;
        }
        return dp[n];
    }

}
